package cn.yunhe.insurance.service.impl;


import cn.yunhe.insurance.dao.ComboDao;
import cn.yunhe.insurance.dao.IcarsDao;
import cn.yunhe.insurance.dao.OrderDao;
import cn.yunhe.insurance.entity.Cars;
import cn.yunhe.insurance.entity.Combo;
import cn.yunhe.insurance.entity.Order;
import cn.yunhe.insurance.entity.User;
import cn.yunhe.insurance.service.OrderService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 苏文秀
 * @date 2018/9/14 17:06
 */
@Service("orderService")
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;
    @Autowired
    private ComboDao comboDao;

    @Override
    public Page<Order> getOrderPage(Map<String, Object> map) {
        Page<Order> page = new Page<Order>();
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        page.setPageSize(pageSize);
        page = PageHelper.startPage(pageNum, pageSize);
        List<Order> orderList = orderDao.getOrderList(map);
        return page;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public boolean addInsurance(Order order) {
        return orderDao.addOrder(order);
    }

    @Override
    public Order getOrderByUserAndCars(Map<String, Object> map) {
        Order order = orderDao.getOrderByUserAndCars(map);
        return order;
    }

    @Override
    public Order getOrderDetail(String oid) {
        return orderDao.getOrderById(oid);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public boolean updateOstate(Order order) {
        return orderDao.updateOstate(order);
    }

    @Override
    public User getUserByIdCard(String idcard) {
        return orderDao.getUserByIdCard(idcard);
    }

    @Override
    public Cars getCarsByLicensenum(String licensenum) {
        return orderDao.getCarsByLicensenum(licensenum);
    }


    @Override
    public Order getOrderInfoByCarLicenseNum(String licenseNum) {
        return orderDao.getOrderInfoByCarLicenseNum(licenseNum);
    }

    @Override
    public List<Combo> getAllCombo() {
        return orderDao.getAllCombo();
    }

    @Override
    public Order getOrderByOuid(String ouid) {
        return orderDao.getOrderByOuid(ouid);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public boolean updateOrderPaystate(Order order) {
        return orderDao.updateOrderPaystate(order);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public boolean updateOrderStatus(Order order) {
        return orderDao.updateOrderStatus(order);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public void export(String[] titles, ServletOutputStream out) {
        try {
            ////创建一个workbook，对应一个Excel文件
            HSSFWorkbook workbook = new HSSFWorkbook();
            /////在webbook中加一个sheet,对应excel中的sheet
            HSSFSheet hssfSheet = workbook.createSheet("sheet1");
            ////在sheet中加头行，
            HSSFRow hssfRow = hssfSheet.createRow(0);
            /////创建单元格  并且设置格式 居中
            HSSFCellStyle hssfCellStyle = workbook.createCellStyle();
            ////设置居中样式
            hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            HSSFCell hssfCell = null;
            for (int i = 0; i < titles.length; i++) {
                hssfCell = hssfRow.createCell(i);
                hssfCell.setCellValue(titles[i]);
                hssfCell.setCellStyle(hssfCellStyle);
            }
            Map<String, Object> map = new HashMap<>();
            List<Order> list = orderDao.getOrderList(map);
            if (list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    hssfRow = hssfSheet.createRow(i + 1);
                    Order order = list.get(i);
                    hssfRow.createCell(0).setCellValue(order.getOid());
                    hssfRow.createCell(1).setCellValue(order.getUser().getSname());
                    hssfRow.createCell(2).setCellValue(order.getUser().getSphone());
                    hssfRow.createCell(3).setCellValue(order.getCars().getLicenseNum());
                    hssfRow.createCell(4).setCellValue(order.getAddtime());
                    String payState = "";
                    if (order.getPaystate() == 0) {
                        payState = "未缴费";
                    } else if (order.getPaystate() == 1) {
                        payState = "已缴费";
                    }
                    hssfRow.createCell(5).setCellValue(payState);
                    String payMethod = "";
                    if (order.getPaymethod() == 0) {
                        payMethod = "未缴费";
                    } else if (order.getPaymethod() == 1) {
                        payMethod = "支付宝";
                    } else if (order.getPaymethod() == 2) {
                        payMethod = "微信";
                    } else if (order.getPaymethod() == 3) {
                        payMethod = "现金";
                    } else if (order.getPaymethod() == 4) {
                        payMethod = "银行卡";
                    }
                    hssfRow.createCell(6).setCellValue(payMethod);
                    hssfRow.createCell(7).setCellValue(order.getEmp().getEmpName());
                    hssfRow.createCell(8).setCellValue(order.getCombo().getCombname());
                    String ostate = "";
                    if (order.getOstate() == 0) {
                        ostate = "正常";
                    } else if (order.getOstate() == 1) {
                        ostate = "退保待审核";
                    } else if (order.getOstate() == 2) {
                        ostate = "退保成功";
                    } else if (order.getOstate() == 3) {
                        ostate = "退保失败";
                    } else if (order.getOstate() == 4) {
                        ostate = "未缴费";
                    }
                    hssfRow.createCell(9).setCellValue(ostate);
                    hssfRow.createCell(10).setCellValue(order.getOremark());
                }
            }
            try {
                workbook.write(out);
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                throw new Exception("导出信息失败!");
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }

    @Override
    public List<Cars> getCarByUser(String idcard) {
        return orderDao.getCarByUser(idcard);
    }

    @Override
    public double getRateByCarPrice(double carPrice) {
        return orderDao.getRateByCarPrice(carPrice);
    }

    @Override
    public double getCombPrice(String comuuid, String licensenum) {
        Combo combo = comboDao.getComboById(comuuid);
        if (combo == null) {
            return 0;
        }
        double combprice = combo.getCombprice();
        if (licensenum == null) {
            return 0;
        }
        double carprice = getCarsByLicensenum(licensenum).getPrice();
        double rate = orderDao.getRateByCarPrice(carprice);
        combprice = combprice*rate;
        return Math.floor(combprice);
    }

    @Override
    public int getOrderByCar(String carNo) {
        return orderDao.getOrderByCar(carNo);
    }


    public ComboDao getComboDao() {
        return comboDao;
    }

    public void setComboDao(ComboDao comboDao) {
        this.comboDao = comboDao;
    }

    public OrderDao getOrderDao() {
        return orderDao;
    }

    public void setOrderDao(OrderDao orderDao) {
        this.orderDao = orderDao;
    }
}
